/*     */ package com.cr.web.controller.system;
/*     */ 
/*     */ import com.cr.common.annotation.Log;
/*     */ import com.cr.common.config.RuoYiConfig;
/*     */ import com.cr.common.core.controller.BaseController;
/*     */ import com.cr.common.core.domain.AjaxResult;
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.enums.BusinessType;
/*     */ import com.cr.common.utils.MyDateUtils;
/*     */ import com.cr.common.utils.ShiroUtils;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ import com.cr.common.utils.file.FileUploadUtils;
/*     */ import com.cr.framework.shiro.service.SysPasswordService;
/*     */ import com.cr.system.service.ISysUserService;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Controller;
/*     */ import org.springframework.ui.ModelMap;
/*     */ import org.springframework.web.bind.annotation.GetMapping;
/*     */ import org.springframework.web.bind.annotation.PostMapping;
/*     */ import org.springframework.web.bind.annotation.RequestMapping;
/*     */ import org.springframework.web.bind.annotation.RequestParam;
/*     */ import org.springframework.web.bind.annotation.ResponseBody;
/*     */ import org.springframework.web.multipart.MultipartFile;
/*     */ 
/*     */ @Controller
/*     */ @RequestMapping({"/system/user/profile"})
/*     */ public class SysProfileController extends BaseController {
/*  37 */   private static final Logger log = LoggerFactory.getLogger(SysProfileController.class);
/*     */   
/*  39 */   private String prefix = "system/user/profile";
/*     */   
/*     */   @Autowired
/*     */   private ISysUserService userService;
/*     */   
/*     */   @Autowired
/*     */   private SysPasswordService passwordService;
/*     */   
/*     */   @GetMapping
/*     */   public String profile(ModelMap mmap) {
/*  53 */     SysUser user = getSysUser();
/*  54 */     mmap.put("user", user);
/*  55 */     mmap.put("roleGroup", this.userService.selectUserRoleGroup(user.getUserId()));
/*  56 */     mmap.put("postGroup", this.userService.selectUserPostGroup(user.getUserId()));
/*  57 */     return this.prefix + "/profile";
/*     */   }
/*     */   
/*     */   @GetMapping({"/checkPassword"})
/*     */   @ResponseBody
/*     */   public boolean checkPassword(String password) {
/*  64 */     SysUser user = getSysUser();
/*  65 */     if (this.passwordService.matches(user, password))
/*  67 */       return true; 
/*  69 */     return false;
/*     */   }
/*     */   
/*     */   @GetMapping({"/resetPwd"})
/*     */   public String resetPwd(ModelMap mmap) {
/*  75 */     SysUser user = getSysUser();
/*  76 */     mmap.put("user", this.userService.selectUserById(user.getUserId()));
/*  77 */     return this.prefix + "/resetPwd";
/*     */   }
/*     */   
/*     */   @Log(title = "重置密码", businessType = BusinessType.UPDATE)
/*     */   @PostMapping({"/resetPwd"})
/*     */   @ResponseBody
/*     */   public AjaxResult resetPwd(String oldPassword, String newPassword) {
/*  85 */     SysUser user = getSysUser();
/*  86 */     if (!this.passwordService.matches(user, oldPassword))
/*  88 */       return error("修改密码失败，旧密码错误"); 
/*  90 */     if (this.passwordService.matches(user, newPassword))
/*  92 */       return error("新密码不能与旧密码相同"); 
/*  94 */     user.setSalt(ShiroUtils.randomSalt());
/*  95 */     user.setPassword(this.passwordService.encryptPassword(user.getLoginName(), newPassword, user.getSalt()));
/*  96 */     user.setPwdUpdateDate(MyDateUtils.getNowDate());
/*  97 */     if (this.userService.resetUserPwd(user) > 0) {
/*  99 */       setSysUser(this.userService.selectUserById(user.getUserId()));
/* 100 */       return success();
/*     */     } 
/* 102 */     return error("修改密码异常，请联系管理员");
/*     */   }
/*     */   
/*     */   @GetMapping({"/edit"})
/*     */   public String edit(ModelMap mmap) {
/* 111 */     SysUser user = getSysUser();
/* 112 */     mmap.put("user", this.userService.selectUserById(user.getUserId()));
/* 113 */     return this.prefix + "/edit";
/*     */   }
/*     */   
/*     */   @GetMapping({"/avatar"})
/*     */   public String avatar(ModelMap mmap) {
/* 122 */     SysUser user = getSysUser();
/* 123 */     mmap.put("user", this.userService.selectUserById(user.getUserId()));
/* 124 */     return this.prefix + "/avatar";
/*     */   }
/*     */   
/*     */   @Log(title = "个人信息", businessType = BusinessType.UPDATE)
/*     */   @PostMapping({"/update"})
/*     */   @ResponseBody
/*     */   public AjaxResult update(SysUser user) {
/* 135 */     SysUser currentUser = getSysUser();
/* 136 */     currentUser.setUserName(user.getUserName());
/* 137 */     currentUser.setEmail(user.getEmail());
/* 138 */     currentUser.setPhonenumber(user.getPhonenumber());
/* 139 */     currentUser.setSex(user.getSex());
/* 140 */     if (StringUtils.isNotEmpty(user.getPhonenumber()) && "1"
/* 141 */       .equals(this.userService.checkPhoneUnique(currentUser)))
/* 143 */       return error("修改用户'" + currentUser.getLoginName() + "'失败，手机号码已存在"); 
/* 145 */     if (StringUtils.isNotEmpty(user.getEmail()) && "1"
/* 146 */       .equals(this.userService.checkEmailUnique(currentUser)))
/* 148 */       return error("修改用户'" + currentUser.getLoginName() + "'失败，邮箱账号已存在"); 
/* 150 */     if (this.userService.updateUserInfo(currentUser) > 0) {
/* 152 */       setSysUser(this.userService.selectUserById(currentUser.getUserId()));
/* 153 */       return success();
/*     */     } 
/* 155 */     return error();
/*     */   }
/*     */   
/*     */   @Log(title = "个人信息", businessType = BusinessType.UPDATE)
/*     */   @PostMapping({"/updateAvatar"})
/*     */   @ResponseBody
/*     */   public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file) {
/* 166 */     SysUser currentUser = getSysUser();
/*     */     try {
/* 169 */       if (!file.isEmpty()) {
/* 171 */         String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
/* 172 */         currentUser.setAvatar(avatar);
/* 173 */         if (this.userService.updateUserInfo(currentUser) > 0) {
/* 175 */           setSysUser(this.userService.selectUserById(currentUser.getUserId()));
/* 176 */           return success();
/*     */         } 
/*     */       } 
/* 179 */       return error();
/* 181 */     } catch (Exception e) {
/* 183 */       log.error("修改头像失败！", e);
/* 184 */       return error(e.getMessage());
/*     */     } 
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\classes\com.zip!\com\cr\web\controller\system\SysProfileController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */